package com.example.backend.controller;

import com.example.backend.common.api.R;
import com.example.backend.common.annotation.OperationLog;
import com.example.backend.common.utils.SecurityUtils;
import com.example.backend.dto.PasswordDTO;
import com.example.backend.dto.ProfileDTO;
import com.example.backend.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 个人中心控制器
 */
@RestController
@RequestMapping("/user/profile")
@RequiredArgsConstructor
public class UserProfileController {
    
    private final UserService userService;
    
    /**
     * 获取个人信息
     */
    @GetMapping
    public R<ProfileDTO> getProfile() {
        Long userId = SecurityUtils.getCurrentUserId();
        return R.ok(userService.getProfile(userId));
    }
    
    /**
     * 更新个人信息
     */
    @PutMapping
    @OperationLog(module = "个人中心", type = "更新信息", description = "更新个人信息")
    public R<Void> updateProfile(@RequestBody @Valid ProfileDTO profileDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        profileDTO.setId(userId);
        return userService.updateProfile(profileDTO) ? R.ok() : R.fail("更新失败");
    }
    
    /**
     * 修改密码
     */
    @PutMapping("/password")
    @OperationLog(module = "个人中心", type = "修改密码", description = "修改个人密码")
    public R<Void> updatePassword(@RequestBody @Valid PasswordDTO passwordDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        return userService.updatePassword(userId, passwordDTO) ? R.ok() : R.fail("修改失败");
    }
} 